<template>
  <div style="" class="plugin" id="divPlugin">

  </div>
</template>

<script>
// import  "@/js/webVideoCtrl.js";
// import  "@/js/jquery-1.7.1.min.js";
// import  "@/js/jsVideoPlugin-1.0.0.min.js";
// import  "@/js/adapter.min.js";
export default {
  name: 'HIKVideo',
  data(){
    return{
      szDeviceIdentify:'',
      g_iWndIndex:0,
      isInit:false,
    }
  },
  props: {
    msg: String
  },
  beforeUnmount() {
    console.info("beforeUnmount")
    WebVideoCtrl.I_StopAllPlay();
    console.log(m_pluginOBJECT.JS_HideWnd());
    // m_pluginOBJECT.JS_Disconnect().then(
    //     () => {
    //       // 断开与插件服务连接成功
    //     },
    //     () => {
    //       // 断开与插件服务连接失败
    //     }
    // );
  },
  mounted() {

  },
  methods: {
    //初始化
    //初始化
    // 全局保存当前选中窗口
    //let g_iWndIndex = 0; //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
    InitPlugin(iWndowType) {
      if(this.isInit){
        return;
      }
      this.isInit = true;
      console.log(WebVideoCtrl);
      let that = this;
      var g_iWndIndex = 0; //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
      // 初始化插件参数及插入插件
      // 初始化插件参数及插入插件
      return new Promise((resolve,reject)=>{
        WebVideoCtrl.I_InitPlugin({
          bWndFull: true,     //是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
          iWndowType: iWndowType,
          cbSelWnd: function (xmlDoc) {
            that.g_iWndIndex = parseInt($(xmlDoc).find("SelectWnd").eq(0).text(), 10);
            var szInfo = "当前选择的窗口编号：" + that.g_iWndIndex;
            console.log(szInfo);
          },
          cbDoubleClickWnd: function (iWndIndex, bFullScreen) {
            var szInfo = "当前放大的窗口编号：" + iWndIndex;
            if (!bFullScreen) {
              szInfo = "当前还原的窗口编号：" + iWndIndex;
            }
            console.log(szInfo);
          },
          cbEvent: function (iEventType, iParam1, iParam2) {
            if (2 == iEventType) {// 回放正常结束
              showCBInfo("窗口" + iParam1 + "回放结束！");
            } else if (-1 == iEventType) {
              showCBInfo("设备" + iParam1 + "网络错误！");
            } else if (3001 == iEventType) {
              clickStopRecord(g_szRecordType, iParam1);
            }
          },
          cbInitPluginComplete: function () {
            console.log("cbInitPluginComplete");

            WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
              resolve(true)
              // 检查插件是否最新
              WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
                if (bFlag) {
                  console.log("检测到新的插件版本，双击开发包目录里的HCWebSDKPlugin.exe升级！");
                }
              });

            }, (e) => {

              resolve(false)
              console.log("插件初始化失败，请确认是否已安装插件；如果未安装，请双击开发包目录里的HCWebSDKPlugin.exe安装！:");
              console.log(e);
            });
          }
        });
      });
    },

    login(szIP, szPort, szUsername, szPassword) {
      let that = this;
      this.szDeviceIdentify = szIP;
      return new Promise((resolve,reject) => {
        WebVideoCtrl.I_Login(
            szIP,
            1,
            szPort,
            szUsername,
            szPassword,
            {
              success: function (xmlDoc) {
                that.$message.success("登录成功")
                console.log('登录成功')
                resolve(true);
              }, error: function () {
                that.$message.error("登录失败")
                console.log('登录失败！');
                resolve(false);
              }
            });
      });

    },

    I_Resize(width,height){
      WebVideoCtrl.I_Resize(width,height);
    },

    hidden(){
      m_pluginOBJECT.JS_HideWnd();
    },
    show(){
      m_pluginOBJECT.JS_ShowWnd();
    },

    getChannel(){


    },
    getZeroChannel(){
      let that = this;
      return new Promise((resolve,reject) => {
        // 零通道
        // 数字通道
        WebVideoCtrl.I_GetDigitalChannelInfo(that.szDeviceIdentify, {
          success: function (xmlDoc) {
            var oChannels = $(xmlDoc).find("InputProxyChannelStatus");
            let channelList = [];
            $.each(oChannels, function (i) {
              var id = $(this).find("id").eq(0).text(),
                  name = $(this).find("name").eq(0).text(),
                  online = $(this).find("online").eq(0).text();
              if ("false" == online) {// 过滤禁用的数字通道
                return true;
              }
              if ("" == name) {
                name = "IPCamera " + (i < 9 ? "0" + (i + 1) : (i + 1));
              }

              channelList.push({
                id:id,
                name:name
              });
            });
            resolve(channelList);
            console.log(" 获取数字通道成功！");
            that.$message.success("获取数字通道成功！")
          },
          error: function (oError) {
            that.$message.error("获取数字通道失败！")
            console.log(" 获取数字通道失败！", oError.errorCode, oError.errorMsg);
            resolve(null);
          }
        });
      });
    },
    startRealPlay(iChannelID,m_iSelWnd) {
      WebVideoCtrl.I_StartRealPlay(this.szDeviceIdentify, {
        iStreamType: 1,
        iChannelID: iChannelID,
        bZeroChannel: false,
        m_iSelWnd:m_iSelWnd,
        success: function () {

        },
        error: function (oError) {

        }
      });
    },
    clickStopRealPlay() {
      console.info(this.g_iWndIndex)
      let oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex),
          szInfo = "";
      if (oWndInfo != null) {
        WebVideoCtrl.I_Stop({
          success: function () {
            szInfo = "停止预览成功！";
            console.log(oWndInfo.szDeviceIdentify + " " + szInfo);
          },
          error: function (oError) {
            console.log(szDeviceIdentify + " 停止预览失败！", oError.errorCode, oError.errorMsg);
          }
        });
      }
    },
    startPlayback(selectBackChanel,startBackTime,endBackTime){
      console.log(selectBackChanel);
      console.log(startBackTime);
      console.log(endBackTime);
      // 2024-05-20 17:29:54
      // 2013-11-11 12:34:56
      let oTransCodeParam = {TransFrameRate: "14", TransResolution: "1", TransBitrate: "19"}
      WebVideoCtrl.I_StartPlayback(this.szDeviceIdentify, {
        iChannelID: selectBackChanel,
        iStreamType: 1,
        szStartTime: startBackTime,
        szEndTime: endBackTime,
        oTransCodeParam: oTransCodeParam,
        success: function () {
          console.log("开始回放成功！");
        },
        error: function (oError) {
          console.log("开始回放失败！");
        }
      });
    },


  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.plugin {
  width: 100%;
  height:  100%;
}
</style>
